package com.sspku.weinotes.controller;

import com.sspku.weinotes.entity.Note;
import com.sspku.weinotes.exception.IncorrectParameterException;
import com.sspku.weinotes.exception.NoSuchIdException;
import com.sspku.weinotes.response.ServerResponse;
import com.sspku.weinotes.service.NoteService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
public class NoteController {

    private static final Logger logger = LoggerFactory.getLogger(NoteController.class);
    @Autowired
    private NoteService noteService;



    /**
     * 获取某笔记本下的所有笔记
     *
     * @param id 笔记本ID
     * @return 笔记集合
     */
    @GetMapping("/notes/{id}")
    public ServerResponse getAllNotes(@PathVariable("id") int id) {
        logger.debug("get all notes by note book id is " + id);
        ServerResponse serverResponse = new ServerResponse();
        List<Note> noteList = noteService.getAllNotes(id);
        if (!noteList.isEmpty()) {
            serverResponse.setDataList(noteList);
        } else {
            serverResponse.setStatus(404);
            serverResponse.setMsg("该笔记本下没有笔记");
        }
        return serverResponse;
    }

    /**
     * 删除笔记
     *
     * @param id 笔记ID
     */
    @DeleteMapping("/note/{id}")
    public void deleteNote(@PathVariable("id") int id) {
        logger.debug("delete note id is " + id);
        noteService.deleteByNoteId(id);
    }


    /**
     * 添加笔记
     *
     * @param noteBookId 笔记本ID
     * @param title      标题
     * @param content    内容
     * @param response   {@link HttpServletResponse}
     */
    @PostMapping("/note")
    public void addNote(int noteBookId, String title, String content, HttpServletResponse response) {
        try {
            if (!noteService.addNote(noteBookId, title, content)) {
                logger.info("add note id error add result null");
                response.setStatus(500);
            }
        } catch (IncorrectParameterException e) {
            logger.info("add note id error ", e);
            response.setStatus(400);
        } catch (NoSuchIdException e) {
            logger.info("add note id error ", e);
            response.setStatus(404);
        }
    }

    /**
     * 修改笔记
     *
     * @param id       笔记ID
     * @param note     笔记Map
     * @param response {@link HttpServletResponse}
     */
    @PatchMapping("/note/{id}")
    public void upNote(@PathVariable("id") int id, @RequestBody Map<String, String> note, HttpServletResponse response) {
        try {
            if (!noteService.modifyNote(id, note.get("title"), note.get("content"))) {
                logger.info("up note error result is null");
                response.setStatus(500);
            }
        } catch (IncorrectParameterException e) {
            logger.info("up note error ", e);
            response.setStatus(400);
        } catch (NoSuchIdException e) {
            logger.info("up note error ", e);
            response.setStatus(404);
        }
    }
    /**
     * 获取某一笔记内容
     *
     * @param id       笔记ID
     *
     */
    @GetMapping("note/{id}")
    public ServerResponse GetNote(@PathVariable("id") Integer id)
    {
        logger.debug("Note id is "+id);
        ServerResponse serverResponse=new ServerResponse();
        Note note=noteService.getNote(id);
        if(note==null)
        {
            serverResponse.setStatus(404);
            serverResponse.setMsg("不存在该笔记");
        }
        else {
            serverResponse.setData(note);
        }
        return serverResponse;
    }
    /**
     * 获取当前笔记本下，没有放入垃圾箱的笔记
     *
     * @param id       笔记本ID
     *
     */
    @GetMapping("get_alive/{id}")
    public ServerResponse GetAlive(@PathVariable("id")Integer id){
        logger.debug("NoteBook id is "+id);
        ServerResponse serverResponse=new ServerResponse();
        List<Note> notes=noteService.getAlive(id);
        serverResponse.setDataList(notes);
        return serverResponse;
    }
    /**
     * 获取当前笔记本下，放入垃圾箱的笔记
     *
     * @param id       笔记本ID
     *
     */
    @GetMapping("get_trash/{id}")
    public ServerResponse GetTrash(@PathVariable("id")Integer id){
        logger.debug("NoteBook id is "+id);
        ServerResponse serverResponse=new ServerResponse();
        List<Note> notes=noteService.getTrash(id);
        serverResponse.setDataList(notes);
        return serverResponse;
    }
    /**
     * 将某一笔记放入垃圾箱
     *
     * @param id       笔记ID
     *
     */
    @PatchMapping("patch_trash/{id}")
    public ServerResponse PatchTrash(@PathVariable("id")Integer id){
        logger.debug("Note id is "+id);
        ServerResponse serverResponse=new ServerResponse(200,"笔记放入垃圾箱成功");
        try{
           int sts= noteService.patchTrash(id);
           if(sts==0) //更新失败
           {
               serverResponse.setStatus(400);
               serverResponse.setMsg("将笔记放入垃圾箱失败");
           }
        }catch(NoSuchIdException|IncorrectParameterException e)
        {
            logger.info("patch_trash error");
            serverResponse.setStatus(400);
            serverResponse.setMsg(e.getMessage());
        }
        return serverResponse;
    }
    /**
     * 将某一笔记移除出垃圾箱
     *
     * @param id       笔记ID
     *
     */
    @PatchMapping("patch_alive/{id}")
    public ServerResponse PatchAlive(@PathVariable("id")Integer id){
        logger.debug("Note id is "+id);
        ServerResponse serverResponse=new ServerResponse(200,"笔记移除出垃圾箱成功");
        try{
            int sts= noteService.patchAlive(id);
            if(sts==0) //更新失败
            {
                serverResponse.setStatus(400);
                serverResponse.setMsg("将笔记移除出垃圾箱失败");
            }
        }catch(NoSuchIdException|IncorrectParameterException e)
        {
            logger.info("patch_trash error");
            serverResponse.setStatus(400);
            serverResponse.setMsg(e.getMessage());
        }
        return serverResponse;
    }
}
